import java.text.DecimalFormat;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Button;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;


public class MainWindow {

	protected Shell shlWhatMyMoney;
	private Client client;
	private String profit;
	private String currentPrice;
	private Label priceLabel;
	private Label profitLabel;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		Display display = Display.getDefault();
		Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
			public void run() {
				try {
					MainWindow window = new MainWindow();window.open();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Open the window.
	 */
	public void open() {
		this.client = new Client();
		Display display = Display.getDefault();
		createContents();
		shlWhatMyMoney.open();
		shlWhatMyMoney.layout();
		while (!shlWhatMyMoney.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shlWhatMyMoney = new Shell();
		shlWhatMyMoney.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		shlWhatMyMoney.setSize(311, 189);
		shlWhatMyMoney.setText("What My Money Made Today :)");
		
		Label FixedLabel2 = new Label(shlWhatMyMoney, SWT.NONE);
		FixedLabel2.setFont(SWTResourceManager.getFont("Bank Gothic", 18, SWT.NORMAL));
		FixedLabel2.setBounds(10, 47, 166, 31);
		FixedLabel2.setText("Current Profit:");
		
		Label FixedLabel2_1 = new Label(shlWhatMyMoney, SWT.NONE);
		FixedLabel2_1.setText("Current Price:");
		FixedLabel2_1.setFont(SWTResourceManager.getFont("Bank Gothic", 18, SWT.NORMAL));
		FixedLabel2_1.setBounds(10, 10, 166, 31);
		
		Button btnNewButton = new Button(shlWhatMyMoney, SWT.NONE);
		btnNewButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				updateData();
			}
		});
		btnNewButton.setFont(SWTResourceManager.getFont("Bank Gothic", 50, SWT.NORMAL));
		btnNewButton.setBounds(20, 84, 281, 73);
		btnNewButton.setText("Refresh");
		
		priceLabel = new Label(shlWhatMyMoney, SWT.NONE);
		priceLabel.setAlignment(SWT.CENTER);
		priceLabel.setText("0$");
		priceLabel.setFont(SWTResourceManager.getFont("Bank Gothic", 18, SWT.NORMAL));
		priceLabel.setBounds(182, 10, 119, 31);
		
		profitLabel = new Label(shlWhatMyMoney, SWT.NONE);
		profitLabel.setAlignment(SWT.CENTER);
		profitLabel.setText("0\u20AA");
		profitLabel.setFont(SWTResourceManager.getFont("Bank Gothic", 18, SWT.NORMAL));
		profitLabel.setBounds(182, 47, 119, 31);
		
		

	}
	
	private void updateData(){
		DecimalFormat twoDecimal = new DecimalFormat("#.##"); 
		double price = this.client.getCurrentPrice();
		this.currentPrice = twoDecimal.format(price) + "$";
		this.profit = twoDecimal.format(client.getProfit()) + "\u20AA";
		
		this.profitLabel.setText(this.profit);
		this.priceLabel.setText(this.currentPrice);
	}
	
}
